package com.jy.datapipeline.importserver.service.impl;

import com.jy.datapipeline.common.constants.ExpConstants;
import com.jy.datapipeline.importserver.common.enums.impl.RetrySubtaskEnum;
import com.jy.datapipeline.common.exception.GlobalException;
import com.jy.datapipeline.importserver.entity.ImportRetrySubtask;
import com.jy.datapipeline.importserver.mapper.ImportRetrySubtaskMapper;
import com.jy.datapipeline.importserver.service.ImportRetrySubtaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @类名 ImportRetrySubtaskServiceImpl
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2023/6/26 10:10
 * @版本 1.0
 */
@Service
public class ImportRetrySubtaskServiceImpl implements ImportRetrySubtaskService {

    @Resource
    private ImportRetrySubtaskMapper importRetrySubtaskMapper;

    @Override
    public void add(ImportRetrySubtask importRetrySubtask) throws GlobalException {
        int res = importRetrySubtaskMapper.insertSelective(importRetrySubtask);
        if (res == 0) {
            throw new GlobalException(ExpConstants.ADD_FAIL);
        }
    }

    @Override
    public List<ImportRetrySubtask> queryUnExecutionRetrySubtasks() {
        return importRetrySubtaskMapper.selectByStatus(RetrySubtaskEnum.UN_EXECUTION.getCode());
    }

    @Override
    public void batchModifyRetrySubtaskStatus(Set<String> retrySubtaskIds) throws GlobalException {
        synchronized (this) {
            int res = importRetrySubtaskMapper.batchUpdateStatus(retrySubtaskIds);
            if(res == 0) {
                throw new GlobalException(ExpConstants.MODIFY_FAIL);
            }
        }
    }

    @Override
    public void deleteBySubtaskIds(Set<Long> subtaskIds) throws GlobalException {
        int res = importRetrySubtaskMapper.deleteBySubtaskIds(subtaskIds);
        if (res == 0) {
            throw new GlobalException(ExpConstants.DELETE_FAIL);
        }
    }
}
